//
//  SISSFormKPIContractNoInputCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/3/4.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSFormKPIContractNoInputCell.h"
#import "SWFormItem.h"
#import "SWFormCompat.h"
#import "UITextField+TextLimit.h"
#import "NSString+SWForm.h"

@interface SISSFormKPIContractNoInputCell ()
///
@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;
@property (strong, nonatomic) UILabel *thirdLabel;
@property (strong, nonatomic) UILabel *fourthLabel;
@property (strong, nonatomic) UILabel *fifthLabel;

@property (strong, nonatomic) UITextField *secondTextField;
@property (strong, nonatomic) UITextField *thirdTextField;
@end
@implementation SISSFormKPIContractNoInputCell


- (void)setItem:(SWFormItem *)item {
    _item = item;
    self.titleLabel.attributedText = item.attributedTitle;
    
    self.textField.text = item.info;
    self.secondTextField.text = item.secondInfo;
    self.thirdTextField.text = item.thirdInfo;
    self.textField.enabled = item.editable;
    self.textField.keyboardType = item.keyboardType;
    self.accessoryType = UITableViewCellAccessoryNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (BOOL)textFieldDidChange {
    if (self.item.maxInputLength > 0) {
        // 限制输入字数
        [self.textField textLimitWithMaxLength:self.item.maxInputLength];
    }
    if (self.firstInputCompletion) {
        self.firstInputCompletion(self.textField.text);
    }
    if (self.secondInputCompletion) {
        self.secondInputCompletion(self.secondTextField.text);
    }
    if (self.thirdInputCompletion) {
        self.thirdInputCompletion(self.thirdTextField.text);
    }
    // 防止输入时表单因刷新动画抖动
    [UIView performWithoutAnimation:^{
        [self.expandableTableView beginUpdates];
        [self.expandableTableView endUpdates];
    }];
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    self.titleLabel.frame = CGRectMake(SW_EdgeMargin, SW_EdgeMargin, SW_TitleWidth, SW_TitleHeight);
    
    //    CGFloat margin = 8.0;
    //    WeakSelf
    //    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(weakSelf.contentView).offset(2 * margin);
    //        make.top.mas_equalTo(weakSelf.contentView).offset(margin);
    //        make.bottom.mas_equalTo(weakSelf.contentView).offset(-margin);
    //    }];
    
//    CGFloat newHeight = [SISSFormKPIContractNoInputCell heightWithItem:self.item];
    self.firstLabel.frame = CGRectMake(SW_EdgeMargin, 1.5*SW_EdgeMargin, SW_TitleHeight, SW_TitleHeight);
    self.textField.placeholder = @"区字号";
    self.textField.textAlignment = NSTextAlignmentCenter;
    self.textField.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight, 1.5 *SW_EdgeMargin, SCREEN_WIDTH * 0.2, SW_TitleHeight);
    self.secondLabel.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight + SCREEN_WIDTH * 0.2, 1.5 *SW_EdgeMargin, SCREEN_WIDTH * 0.25, SW_TitleHeight);
    self.secondTextField.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight + SCREEN_WIDTH * 0.35, 1.5 *SW_EdgeMargin, SCREEN_WIDTH * 0.25, SW_TitleHeight);
    self.thirdLabel.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight + SCREEN_WIDTH * 0.7, 1.5 *SW_EdgeMargin, SW_TitleHeight, SW_TitleHeight);
    
    self.fourthLabel.frame = CGRectMake(SW_EdgeMargin, 2*SW_TitleHeight, SW_TitleHeight, SW_TitleHeight);
    self.thirdTextField.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight,  2*SW_TitleHeight, SCREEN_WIDTH * 0.3, SW_TitleHeight);
     self.fifthLabel.frame = CGRectMake(SW_EdgeMargin + SW_TitleHeight + SCREEN_WIDTH * 0.3,  2*SW_TitleHeight, SW_TitleHeight, SW_TitleHeight);
}



- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.textField.text = self.item.info;
}



- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.textField.text = self.item.info;
    if ([self.delegete respondsToSelector:@selector(contentDidChangeTextWithCell:text:)]) {
        [self.delegete contentDidChangeTextWithCell:self text:textField.text];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self.delegete respondsToSelector:@selector(contentShouldChangeTextWithCell:text:)]) {
        return [self.delegete contentShouldChangeTextWithCell:self text:[NSString stringWithFormat:@"%@%@",textField.text,string]];
    }
    return YES;
}

+ (CGFloat)heightWithItem:(SWFormItem *)item {
    return 80;
    //    CGFloat infoHeight = [item.info sizeWithFontSize:SW_InfoFont maxSize:CGSizeMake(SW_SCRREN_WIDTH - (SW_TitleWidth + 7*SW_EdgeMargin), MAXFLOAT)].height;
    //    return MAX(item.defaultHeight, infoHeight + 2*SW_EdgeMargin);
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (UILabel *)firstLabel {
    if (!_firstLabel) {
        _firstLabel = [UILabel yyr_labelWithText:@"深(" fontSize:13 textColor:MAIN_TEXT_COLOR];
        _firstLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_firstLabel];
    }
    return _firstLabel;
}

- (UILabel *)secondLabel {
    if (!_secondLabel) {
        _secondLabel = [UILabel yyr_labelWithText:@")房现买字(" fontSize:13 textColor:MAIN_TEXT_COLOR];
        _secondLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_secondLabel];
    }
    return _secondLabel;
}

- (UILabel *)thirdLabel {
    if (!_thirdLabel) {
        _thirdLabel = [UILabel yyr_labelWithText:@")" fontSize:13 textColor:MAIN_TEXT_COLOR];
        _thirdLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_thirdLabel];
    }
    return _thirdLabel;
}

- (UILabel *)fourthLabel {
    if (!_fourthLabel) {
        _fourthLabel = [UILabel yyr_labelWithText:@"第" fontSize:13 textColor:MAIN_TEXT_COLOR];
        _fourthLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_fourthLabel];
    }
    return _fourthLabel;
}

- (UILabel *)fifthLabel {
    if (!_fifthLabel) {
        _fifthLabel = [UILabel yyr_labelWithText:@"号" fontSize:13 textColor:MAIN_TEXT_COLOR];
        _fifthLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:_fifthLabel];
    }
    return _fifthLabel;
}

- (UITextField *)secondTextField {
    if (!_secondTextField) {
        _secondTextField = [[UITextField alloc] init];
        _secondTextField.placeholder = @"合同年份";
        _secondTextField.font = [UIFont boldSystemFontOfSize:13];
        _secondTextField.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_secondTextField];
    }
    return _secondTextField;
}

- (UITextField *)thirdTextField {
    if (!_thirdTextField) {
        _thirdTextField = [[UITextField alloc] init];
        _thirdTextField.placeholder = @"合同序号";
        _thirdTextField.font = [UIFont boldSystemFontOfSize:13];
        _thirdTextField.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_thirdTextField];
    }
    return _thirdTextField;
}

@end


@implementation UITableView (SISSFormKPIContractNoInputCell)

- (SISSFormKPIContractNoInputCell *)inputKPIContractNoInputCellWithId:(NSString *)cellId {
    SISSFormKPIContractNoInputCell *cell = [self dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[SISSFormKPIContractNoInputCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.expandableTableView = self;
    }
    return cell;
}

@end
